package com.liehacker.android.etcs;


import com.liehacker.android.util.DebugLog;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View.MeasureSpec;
import android.widget.VideoView;


public class SimpleVideoView extends VideoView {
	private boolean wideMode = true;
	private boolean fullScreen = false;
	private int width;
	private int height;
	
	MediaPlayer.OnVideoSizeChangedListener lsnr;
	private int oldWidth;
	private int oldHeight;

	public SimpleVideoView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub	
		
	}	
	
	public SimpleVideoView(Context context, AttributeSet attr) {
		super(context, attr);
	}
	
	public void setWideMode(boolean flag) {
		wideMode = flag;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);	
		
		DisplayMetrics displayMetrics = new DisplayMetrics();
		Activity.class.cast(getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		if(wideMode) {//16:9
			try {
				if(fullScreen){
					width = displayMetrics.widthPixels;
					height = displayMetrics.heightPixels;
				}else{
					width = MeasureSpec.getSize(widthMeasureSpec);
					height = width * 9 / 16;
//					super.onMeasure(widthMeasureSpec, heightMeasureSpec);
				}
				DebugLog.d("video", "16:9 onMeasure, wide : width = "+width+" height = "+height);
				setMeasuredDimension(width, height);
			} catch (Throwable t) {
				t.printStackTrace();
			}
		} else {//4:3
			if(fullScreen){
				width = displayMetrics.widthPixels;
				height = displayMetrics.heightPixels;
				width = height*4/3;
			}else{
				width = MeasureSpec.getSize(widthMeasureSpec);
				height = width * 3 / 4;
			}
			DebugLog.d("video", "4:3 onMeasure, normal : width = "+width+" height = "+height);
			setMeasuredDimension(width, height);
		}
		
	}
	
}
